import { subscribeWithSelector } from 'zustand/middleware'
import create from 'zustand'
import { IPermission } from '@/pages/sysConfig/virtualRoomConfig/interface'
import { useGlobalStore } from '@/store/global'
import { t } from 'i18next'

export type ITab = 'basic' | 'params' | 'vmp' | 'poll' | 'mix' | 'tvwall' | 'vl' | 'dcs' | 'ai'

type StateStore = {
	tab: ITab
	tabData: {
		label: string
		value: ITab
		hidden?: boolean
	}[]
	/** 会议moid或者模板moid */
	meetingMoid: string
	/** 是否是会议模板 */
	isTemplate: boolean
	preKey: string

	maxDayForAllowBookMeeting: string
	systemDateTime: string
	systemDate: string
	meetingFormKey: string
	/** 会议状态
    1-预约;
    2-开始;
    3-结束;
    6-待审批;
    7-审批不通过; */
	state: 1 | 2 | 3 | 6 | 7
	/** 是否保存为模板 */
	_isSaveAsTemplate: boolean

	/** 权限 */
	permission: IPermission
	resources: {
		name: string
		max_join_mt: number
		total: number
		used: number
	}
	/** 使select下拉框跟随容器滚动 */
	_container: HTMLDivElement | null
}

type IStore = StateStore & {
	update: (d: Partial<StateStore>) => void
	reset: () => void
	initPermission: () => void
}

const initialState: StateStore = {
	tab: 'basic',
	tabData: [],
	meetingMoid: '',
	isTemplate: false,

	maxDayForAllowBookMeeting: '',
	systemDateTime: '',
	systemDate: '',
	meetingFormKey: '',
	preKey: '',
	state: 1,
	_isSaveAsTemplate: false,
	permission: {
		tradMix: 0,
		portMix: 0,
		enableVRS: false,
		enableDCS: false,
		enableLive: false,
		enableVideo: false,
		enableQuantum: false,
		enablePortMedia: false,
		enableSfu: false,
		enableTrans: false,
		enableHDMeeting: false,
		certificate: false,
		integrated: false,
		hasAuthenticationServer: false,
		sm1: false,
		'60fps': false,
		'4K': false,
		h265: false,
		has60FpsInH265: false,
	},
	resources: {
		name: t('大型会议'),
		max_join_mt: 192,
		total: 1000,
		used: 0,
	},
	_container: null,
}

export const useCreateContentStore = create<IStore>()(
	subscribeWithSelector((set, get) => ({
		...initialState,
		update: (d) => set({ ...d }),
		reset: () => set(initialState),
		initPermission: () => {
			const { update } = get()
			const serverInfo = useGlobalStore.getState().serverInfo
			const user = useGlobalStore.getState().user
			update({
				//更新权限信息
				permission: {
					tradMix: serverInfo?.trad_mix || 0,
					portMix: serverInfo?.port_mix || 0,
					enableVRS: !!user?.enable_vrs,
					enableDCS: !!user?.enable_dcs,
					enableLive: !!user?.enable_live,
					enableVideo: !!user?.enable_video,
					enableQuantum: !!serverInfo?.enable_quantum,
					enablePortMedia: !!user?.enable_port_media,
					enableSfu: !!user?.enable_sfu,
					enableTrans: !!user?.enable_trans,
					enableHDMeeting: !!user?.enable_hd_meeting,
					certificate: !!serverInfo?.enable_sm4,
					integrated: !!serverInfo?.integrated,
					hasAuthenticationServer: !!serverInfo?.has_auth_server,
					sm1: !!serverInfo?.enable_sm1,
					'60fps': !!serverInfo?.enable60fps,
					'4K': !!serverInfo?.enable4k,
					h265: !!user?.enable_h265,
					has60FpsInH265: !!serverInfo?.enable60fps && !!user?.enable_h265 && !!!serverInfo?.jd2000,
				},
			})
		},
	}))
)
